home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / perl5 / Dpkg / Substvars.pm < prev    next >
Encoding:
Perl POD Document  |  2012-09-17  |  7.0 KB  |  303 lines

  1. # Copyright ┬⌐ 2007-2010 Rapha├½l Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15.  
  16. package Dpkg::Substvars;
  17.  
  18. use strict;
  19. use warnings;
  20.  
  21. our $VERSION = "1.00";
  22.  
  23. use Dpkg qw($version);
  24. use Dpkg::Arch qw(get_host_arch);
  25. use Dpkg::ErrorHandling;
  26. use Dpkg::Gettext;
  27.  
  28. use POSIX qw(:errno_h);
  29.  
  30. use base qw(Dpkg::Interface::Storable);
  31.  
  32. my $maxsubsts = 50;
  33.  
  34. =encoding utf8
  35.  
  36. =head1 NAME
  37.  
  38. Dpkg::Substvars - handle variable substitution in strings
  39.  
  40. =head1 DESCRIPTION
  41.  
  42. It provides some an object which is able to substitute variables in
  43. strings.
  44.  
  45. =head1 METHODS
  46.  
  47. =over 8
  48.  
  49. =item my $s = Dpkg::Substvars->new($file)
  50.  
  51. Create a new object that can do substitutions. By default it contains
  52. generic substitutions like ${Newline}, ${Space}, ${Tab}, ${dpkg:Version}
  53. and ${dpkg:Upstream-Version}.
  54.  
  55. Additional substitutions will be read from the $file passed as parameter.
  56.  
  57. It keeps track of which substitutions were actually used (only counting
  58. substvars(), not get()), and warns about unused substvars when asked to. The
  59. substitutions that are always present are not included in these warnings.
  60.  
  61. =cut
  62.  
  63. sub new {
  64.     my ($this, $arg) = @_;
  65.     my $class = ref($this) || $this;
  66.     my $self = {
  67.         vars => {
  68.             "Newline" => "\n",
  69.             "Space" => " ",
  70.             "Tab" => "\t",
  71.             "dpkg:Version" => $version,
  72.             "dpkg:Upstream-Version" => $version,
  73.             },
  74.         used => {},
  75.     msg_prefix => "",
  76.     };
  77.     $self->{'vars'}{'dpkg:Upstream-Version'} =~ s/-[^-]+$//;
  78.     bless $self, $class;
  79.     $self->no_warn($_) foreach keys %{$self->{'vars'}};
  80.     if ($arg) {
  81.         $self->load($arg) if -e $arg;
  82.     }
  83.     return $self;
  84. }
  85.  
  86. =item $s->set($key, $value)
  87.  
  88. Add/replace a substitution.
  89.  
  90. =cut
  91.  
  92. sub set {
  93.     my ($self, $key, $value) = @_;
  94.     $self->{'vars'}{$key} = $value;
  95. }
  96.  
  97. =item $s->get($key)
  98.  
  99. Get the value of a given substitution.
  100.  
  101. =cut
  102.  
  103. sub get {
  104.     my ($self, $key) = @_;
  105.     return $self->{'vars'}{$key};
  106. }
  107.  
  108. =item $s->delete($key)
  109.  
  110. Remove a given substitution.
  111.  
  112. =cut
  113.  
  114. sub delete {
  115.     my ($self, $key) = @_;
  116.     delete $self->{'used'}{$key};
  117.     return delete $self->{'vars'}{$key};
  118. }
  119.  
  120. =item $s->no_warn($key)
  121.  
  122. Prevents warnings about a unused substitution, for example if it is provided by
  123. default.
  124.  
  125. =cut
  126.  
  127. sub no_warn {
  128.     my ($self, $key) = @_;
  129.     $self->{'used'}{$key}++;
  130. }
  131.  
  132. =item $s->load($file)
  133.  
  134. Add new substitutions read from $file.
  135.  
  136. =item $s->parse($fh, $desc)
  137.  
  138. Add new substitutions read from the filehandle. $desc is used to identify
  139. the filehandle in error messages.
  140.  
  141. =cut
  142.  
  143. sub parse {
  144.     my ($self, $fh, $varlistfile) = @_;
  145.     binmode($fh);
  146.     while (<$fh>) {
  147.     next if m/^\s*\#/ || !m/\S/;
  148.     s/\s*\n$//;
  149.     m/^(\w[-:0-9A-Za-z]*)\=(.*)$/ ||
  150.         error(_g("bad line in substvars file %s at line %d"),
  151.           $varlistfile, $.);
  152.     $self->{'vars'}{$1} = $2;
  153.     }
  154. }
  155.  
  156. =item $s->set_version_substvars($version)
  157.  
  158. Defines ${binary:Version}, ${source:Version} and
  159. ${source:Upstream-Version} based on the given version string.
  160.  
  161. These will never be warned about when unused.
  162.  
  163. =cut
  164.  
  165. sub set_version_substvars {
  166.     my ($self, $version) = @_;
  167.  
  168.     $self->{'vars'}{'binary:Version'} = $version;
  169.     $self->{'vars'}{'source:Version'} = $version;
  170.     $self->{'vars'}{'source:Version'} =~ s/\+b[0-9]+$//;
  171.     $self->{'vars'}{'source:Upstream-Version'} = $version;
  172.     $self->{'vars'}{'source:Upstream-Version'} =~ s/-[^-]*$//;
  173.  
  174.     # XXX: Source-Version is now deprecated, remove in the future.
  175.     $self->{'vars'}{'Source-Version'} = $version;
  176.  
  177.     $self->no_warn($_) foreach qw/binary:Version source:Version source:Upstream-Version Source-Version/;
  178. }
  179.  
  180. =item $s->set_arch_substvars()
  181.  
  182. Defines architecture variables: ${Arch}.
  183.  
  184. This will never be warned about when unused.
  185.  
  186. =cut
  187.  
  188. sub set_arch_substvars {
  189.     my ($self) = @_;
  190.  
  191.     $self->{'vars'}{'Arch'} = get_host_arch();
  192.     $self->no_warn('Arch');
  193. }
  194.  
  195. =item $newstring = $s->substvars($string)
  196.  
  197. Substitutes variables in $string and return the result in $newstring.
  198.  
  199. =cut
  200.  
  201. sub substvars {
  202.     my ($self, $v, %opts) = @_;
  203.     my $lhs;
  204.     my $vn;
  205.     my $rhs = '';
  206.     my $count = 0;
  207.     $opts{msg_prefix} = $self->{msg_prefix} unless exists $opts{msg_prefix};
  208.     $opts{no_warn} = 0 unless exists $opts{no_warn};
  209.  
  210.     while ($v =~ m/^(.*?)\$\{([-:0-9a-z]+)\}(.*)$/si) {
  211.         # If we have consumed more from the leftover data, then
  212.         # reset the recursive counter.
  213.         $count = 0 if (length($3) < length($rhs));
  214.  
  215.         $count < $maxsubsts ||
  216.             error($opts{msg_prefix} .
  217.               _g("too many substitutions - recursive ? - in \`%s'"), $v);
  218.         $lhs = $1; $vn = $2; $rhs = $3;
  219.         if (defined($self->{'vars'}{$vn})) {
  220.             $v = $lhs . $self->{'vars'}{$vn} . $rhs;
  221.         $self->no_warn($vn);
  222.             $count++;
  223.         } else {
  224.             warning($opts{msg_prefix} . _g("unknown substitution variable \${%s}"),
  225.                 $vn) unless $opts{no_warn};
  226.             $v = $lhs . $rhs;
  227.         }
  228.     }
  229.     return $v;
  230. }
  231.  
  232. =item $s->warn_about_unused()
  233.  
  234. Issues warning about any variables that were set, but not used
  235.  
  236. =cut
  237.  
  238. sub warn_about_unused {
  239.     my ($self, %opts) = @_;
  240.     $opts{msg_prefix} = $self->{msg_prefix} unless exists $opts{msg_prefix};
  241.  
  242.     foreach my $vn (keys %{$self->{'vars'}}) {
  243.         next if $self->{'used'}{$vn};
  244.         # Empty substitutions variables are ignored on the basis
  245.         # that they are not required in the current situation
  246.         # (example: debhelper's misc:Depends in many cases)
  247.         next if $self->{'vars'}{$vn} eq "";
  248.         warning($opts{msg_prefix} . _g("unused substitution variable \${%s}"), $vn);
  249.     }
  250. }
  251.  
  252. =item $s->set_msg_prefix($prefix)
  253.  
  254. Define a prefix displayed before all warnings/error messages output
  255. by the module.
  256.  
  257. =cut
  258.  
  259. sub set_msg_prefix {
  260.     my ($self, $prefix) = @_;
  261.     $self->{msg_prefix} = $prefix;
  262. }
  263.  
  264. =item $s->save($file)
  265.  
  266. Store all substitutions variables except the automatic ones in the
  267. indicated file.
  268.  
  269. =item "$s"
  270.  
  271. Return a string representation of all substitutions variables except the
  272. automatic ones.
  273.  
  274. =item $str = $s->output($fh)
  275.  
  276. Print all substitutions variables except the automatic ones in the
  277. filehandle and return the content written.
  278.  
  279. =cut
  280.  
  281. sub output {
  282.     my ($self, $fh) = @_;
  283.     my $str = "";
  284.     # Store all non-automatic substitutions only
  285.     foreach my $vn (sort keys %{$self->{'vars'}}) {
  286.     next if /^(?:(?:dpkg|source|binary):(?:Source-)?Version|Space|Tab|Newline|Arch|Source-Version|F:.+)$/;
  287.     my $line = "$vn=" . $self->{vars}{$vn} . "\n";
  288.     print $fh $line if defined $fh;
  289.     $str .= $line;
  290.     }
  291.     return $str;
  292. }
  293.  
  294. =back
  295.  
  296. =head1 AUTHOR
  297.  
  298. Rapha├½l Hertzog <hertzog@debian.org>.
  299.  
  300. =cut
  301.  
  302. 1;
  303.